import type { Router } from "express";
import type { Ioracle, IcallbackFn } from "../../types";
import { decrypt } from "../../utils/privateKey";

interface Measure {
  rowid?: string;
  dept_code: string;
  measurement_header: string;
  measurement_detail: string;
  username: string;
}

interface Judger {
  rowid?: string;
  dept_code: string;
  name: string;
  badge: string;
  judger_dept: string;
  judger_name: string;
  judger_badge: string;
  username: string;
}

const MeasureSet = (
  router: Router,
  oracle: Ioracle,
  callbackFn: IcallbackFn
) => {
  router.post("/measurelist", (req, res) => {
    const { dept } = req.body;
    let sql = ` select a.rowid,a.measurement_header,a.measurement_detail,b.chinese_name created_by,c.chinese_name last_update_by,a.last_update_time
     from xxcc_behavior_mesure_list a,user_info_n b,user_info_n c where dept_code = '${dept}' and a.created_by = b.account
     and a.last_update_by = c.account order by a.measure_id`;
    callbackFn(res, oracle.connHrm, { sql });
  });

  router.post("/setmeasure", async (req, res) => {
    const { param, type } = req.body;
    let paramParse: Measure = JSON.parse(param);
    let sql = "";
    if (type === "add") {
      try {
        paramParse.username = decrypt(paramParse.username);
        sql = ` select count(*) count from xxcc_behavior_mesure_list where dept_code = '${paramParse.dept_code}' and measurement_header = '${paramParse.measurement_header}'`;
        const result = await oracle.connHrm({ sql });
        if (result.data[0].count === 0) {
          sql = ` insert into xxcc_behavior_mesure_list(measure_id,dept_code,measurement_header,measurement_detail,created_by,last_update_by,last_update_time)
         values(xxcc_behavior_judgelist_s.nextval,'${paramParse.dept_code}','${paramParse.measurement_header}','${paramParse.measurement_detail}'
         ,'${paramParse.username}','${paramParse.username}',sysdate) `;
          const res1 = await oracle.connHrm({ sql });
          if (res1.code === 200) {
            sql = ` select a.rowid,b.chinese_name from xxcc_behavior_mesure_list a,user_info_n b where a.dept_code = '${paramParse.dept_code}' 
             and a.measurement_header = '${paramParse.measurement_header}' and a.created_by = b.account`;
            callbackFn(res, oracle.connHrm, { sql });
          }
        } else {
          res.send({ code: 0, message: "此單位已存在該行為準則" });
        }
      } catch {
        res.send({ code: 0, message: "数据库异常" });
      }
    } else if (type === "modify") {
      try {
        paramParse.username = decrypt(paramParse.username);
        sql = ` update xxcc_behavior_mesure_list set measurement_detail = '${paramParse.measurement_detail}',last_update_by
         ='${paramParse.username}',last_update_time = sysdate where rowid = '${paramParse.rowid}'`;
        callbackFn(res, oracle.connHrm, { sql });
      } catch {
        res.send({ code: 0, message: "後台異常" });
      }
    } else if (type === "delete") {
      sql = ` delete from xxcc_behavior_mesure_list where rowid = '${paramParse.rowid}'`;
      callbackFn(res, oracle.connHrm, { sql });
    }
  });

  router.post("/judgerlist", (req, res) => {
    const { dept } = req.body;
    let sql = ` select rowid,dept_code,badge,name,judger_dept,judger_badge,judger_name from xxcc_behavior_judge_set where dept_code = '${dept}'`;
    callbackFn(res, oracle.connHrm, { sql });
  });

  router.post("/setjudger", async (req, res) => {
    const { param, type } = req.body;
    let paramParse: Judger = JSON.parse(param);
    let sql = "";
    if (type === "add") {
      try {
        paramParse.username = decrypt(paramParse.username);
        sql = ` select count(*) count from xxcc_behavior_judge_set where badge = '${paramParse.badge}'`;
        const result = await oracle.connHrm({ sql });
        if (result.data[0].count === 0) {
          sql = ` insert into xxcc_behavior_judge_set(dept_code,badge,name,judger_dept,judger_badge,judger_name,created_by,last_update_by,last_update_time)
         values('${paramParse.dept_code}','${paramParse.badge}','${paramParse.name}','${paramParse.judger_dept}','${paramParse.judger_badge}'
         ,'${paramParse.judger_name}','${paramParse.username}','${paramParse.username}',sysdate) `;
          const res1 = await oracle.connHrm({ sql });
          if (res1.code === 200) {
            sql = ` select a.rowid,b.chinese_name from xxcc_behavior_judge_set a,user_info_n b where a.badge = '${paramParse.badge}' 
              and a.badge = b.work_num`;
            callbackFn(res, oracle.connHrm, { sql });
          }
        } else {
          res.send({ code: 0, message: "此人員已設置，請確認" });
        }
      } catch {
        res.send({ code: 0, message: "数据库异常" });
      }
    } else if (type === "modify") {
      try {
        paramParse.username = decrypt(paramParse.username);
        sql = ` update xxcc_behavior_judge_set set dept_code = '${paramParse.dept_code}',badge='${paramParse.badge}',name='${paramParse.name}'
        ,judger_dept = '${paramParse.judger_dept}',judger_badge='${paramParse.judger_badge}',judger_name='${paramParse.judger_name}'
        ,last_update_by ='${paramParse.username}',last_update_time = sysdate where rowid = '${paramParse.rowid}'`;
        callbackFn(res, oracle.connHrm, { sql });
      } catch {
        res.send({ code: 0, message: "後台異常" });
      }
    } else if (type === "delete") {
      sql = ` delete from xxcc_behavior_judge_set where rowid = '${paramParse.rowid}'`;
      callbackFn(res, oracle.connHrm, { sql });
    }
  });

  return router;
};

export { MeasureSet };
